/*
 * Created by SharpDevelop.
 * User: michaelo
 * Date: 18/05/2005
 * Time: 19:01
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Xml.Schema;
using Orz.LogManager.Config;


namespace Orz.LogManager
{
	
	
/// <summary>
/// Description of LogsetManagerForm.
/// </summary>
public class LogsetManagerForm : System.Windows.Forms.Form
{
		private System.ComponentModel.IContainer components;
		private System.Windows.Forms.ColumnHeader colName;
		private System.Windows.Forms.GroupBox groupBox1;
		private System.Windows.Forms.Button cmdNew;
		private System.Windows.Forms.ImageList imageList1;
		private System.Windows.Forms.ColumnHeader colDescription;
		private System.Windows.Forms.Button cmdExplore;
		private System.Windows.Forms.Button cmdEdit;
		private System.Windows.Forms.Button cmdCancel;
		private System.Windows.Forms.Button cmdOpen;
		private System.Windows.Forms.Label lblMain;
		private System.Windows.Forms.Button cmdDelete;
		private System.Windows.Forms.ListView lvwLogsets;
	

	
	private IList logsets_i;
	private LogsetWithFile selectedLogset_i;
	
	
	private HelpForm help_i;
	
	
	public LogsetManagerForm()
	{
		//
		// The InitializeComponent() call is required for Windows Forms designer support.
		//
		InitializeComponent();
		
		help_i = new HelpForm (this);
				
		try
		{
			logsets_i = LogsetLoader.GetLogsets();		
			PopulateLogsetList (logsets_i);		
		}
		catch (Exception e)
		{
			MessageBox.Show (e.Message, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
	}
	
	
	private void OpenSelectedLogset()
	{
		if (0 == lvwLogsets.Items.Count  ||  0 == lvwLogsets.SelectedIndices.Count) return;
		
		int iIndex = lvwLogsets.SelectedIndices[0];
		selectedLogset_i = (LogsetWithFile) logsets_i[iIndex];
		this.Close();	
	}
	
	
	
	public LogsetWithFile SelectedLogset
	{
		get { return selectedLogset_i; }
	}
	
	
	
	#region Windows Forms Designer generated code
	/// <summary>
	/// This method is required for Windows Forms designer support.
	/// Do not change the method contents inside the source code editor. The Forms designer might
	/// not be able to load this method if it was changed manually.
	/// </summary>
	private void InitializeComponent() {
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(LogsetManagerForm));
			this.lvwLogsets = new System.Windows.Forms.ListView();
			this.cmdDelete = new System.Windows.Forms.Button();
			this.lblMain = new System.Windows.Forms.Label();
			this.cmdOpen = new System.Windows.Forms.Button();
			this.cmdCancel = new System.Windows.Forms.Button();
			this.cmdEdit = new System.Windows.Forms.Button();
			this.cmdExplore = new System.Windows.Forms.Button();
			this.colDescription = new System.Windows.Forms.ColumnHeader();
			this.imageList1 = new System.Windows.Forms.ImageList(this.components);
			this.cmdNew = new System.Windows.Forms.Button();
			this.groupBox1 = new System.Windows.Forms.GroupBox();
			this.colName = new System.Windows.Forms.ColumnHeader();
			this.groupBox1.SuspendLayout();
			this.SuspendLayout();
			// 
			// lvwLogsets
			// 
			this.lvwLogsets.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
						this.colName,
						this.colDescription});
			this.lvwLogsets.FullRowSelect = true;
			this.lvwLogsets.GridLines = true;
			this.lvwLogsets.HideSelection = false;
			this.lvwLogsets.Location = new System.Drawing.Point(16, 56);
			this.lvwLogsets.MultiSelect = false;
			this.lvwLogsets.Name = "lvwLogsets";
			this.lvwLogsets.Size = new System.Drawing.Size(368, 192);
			this.lvwLogsets.SmallImageList = this.imageList1;
			this.lvwLogsets.TabIndex = 1;
			this.lvwLogsets.Tag = @"A configuration file stores details of each of the log files you want to view at once.  For example, you may want to view your JBoss server log, Tomcat access log, and some other database log all at the same time.  Your configuration file will contain the locationsof each of these log files making it easier for you to open them all next time.  If this is your first time using Log Manager just click the 'New...' button below to set up a configuration file.";
			this.lvwLogsets.View = System.Windows.Forms.View.Details;
			this.lvwLogsets.DoubleClick += new System.EventHandler(this.LvwLogsetsDoubleClick);
			// 
			// cmdDelete
			// 
			this.cmdDelete.Location = new System.Drawing.Point(312, 256);
			this.cmdDelete.Name = "cmdDelete";
			this.cmdDelete.TabIndex = 2;
			this.cmdDelete.Tag = "Deletes the selected configuration file.";
			this.cmdDelete.Text = "&Delete";
			this.cmdDelete.Click += new System.EventHandler(this.CmdDeleteClick);
			// 
			// lblMain
			// 
			this.lblMain.Location = new System.Drawing.Point(16, 24);
			this.lblMain.Name = "lblMain";
			this.lblMain.Size = new System.Drawing.Size(360, 32);
			this.lblMain.TabIndex = 0;
			this.lblMain.Text = "Select a set of log files to view, or click \'New...\' to create a new configuratio" +
"n.";
			// 
			// cmdOpen
			// 
			this.cmdOpen.DialogResult = System.Windows.Forms.DialogResult.OK;
			this.cmdOpen.Location = new System.Drawing.Point(416, 16);
			this.cmdOpen.Name = "cmdOpen";
			this.cmdOpen.TabIndex = 2;
			this.cmdOpen.Tag = "Opens all the log files in the selected configuration file.";
			this.cmdOpen.Text = "&Open";
			this.cmdOpen.Click += new System.EventHandler(this.CmdOpenClick);
			// 
			// cmdCancel
			// 
			this.cmdCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
			this.cmdCancel.Location = new System.Drawing.Point(416, 48);
			this.cmdCancel.Name = "cmdCancel";
			this.cmdCancel.TabIndex = 2;
			this.cmdCancel.Tag = "Closes Log Manager.";
			this.cmdCancel.Text = "&Cancel";
			this.cmdCancel.Click += new System.EventHandler(this.CmdCloseClick);
			// 
			// cmdEdit
			// 
			this.cmdEdit.Location = new System.Drawing.Point(224, 256);
			this.cmdEdit.Name = "cmdEdit";
			this.cmdEdit.TabIndex = 2;
			this.cmdEdit.Tag = "Displays details of all the individual log files referenced by the selected confi" +
"guration file, and lets you edit them or add new ones.";
			this.cmdEdit.Text = "&Properties...";
			this.cmdEdit.Click += new System.EventHandler(this.CmdEditClick);
			// 
			// cmdExplore
			// 
			this.cmdExplore.Location = new System.Drawing.Point(416, 104);
			this.cmdExplore.Name = "cmdExplore";
			this.cmdExplore.TabIndex = 2;
			this.cmdExplore.Tag = "Lets you open a configuration file using Windows Explorer.";
			this.cmdExplore.Text = "&Explore...";
			this.cmdExplore.Click += new System.EventHandler(this.CmdExploreClick);
			// 
			// colDescription
			// 
			this.colDescription.Text = "Description";
			this.colDescription.Width = 200;
			// 
			// imageList1
			// 
			this.imageList1.ImageSize = new System.Drawing.Size(16, 16);
			this.imageList1.ImageStream = ((System.Windows.Forms.ImageListStreamer)(resources.GetObject("imageList1.ImageStream")));
			this.imageList1.TransparentColor = System.Drawing.Color.Transparent;
			// 
			// cmdNew
			// 
			this.cmdNew.Location = new System.Drawing.Point(136, 256);
			this.cmdNew.Name = "cmdNew";
			this.cmdNew.TabIndex = 2;
			this.cmdNew.Tag = "Creates a new configuration file and allows you to add log files to it.";
			this.cmdNew.Text = "&New...";
			this.cmdNew.Click += new System.EventHandler(this.CmdNewClick);
			// 
			// groupBox1
			// 
			this.groupBox1.Controls.Add(this.lvwLogsets);
			this.groupBox1.Controls.Add(this.cmdDelete);
			this.groupBox1.Controls.Add(this.cmdEdit);
			this.groupBox1.Controls.Add(this.cmdNew);
			this.groupBox1.Controls.Add(this.lblMain);
			this.groupBox1.Location = new System.Drawing.Point(8, 8);
			this.groupBox1.Name = "groupBox1";
			this.groupBox1.Size = new System.Drawing.Size(400, 296);
			this.groupBox1.TabIndex = 3;
			this.groupBox1.TabStop = false;
			// 
			// colName
			// 
			this.colName.Text = "Configuration file";
			this.colName.Width = 145;
			// 
			// LogsetManagerForm
			// 
			this.AcceptButton = this.cmdOpen;
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
			this.CancelButton = this.cmdCancel;
			this.ClientSize = new System.Drawing.Size(498, 312);
			this.Controls.Add(this.groupBox1);
			this.Controls.Add(this.cmdOpen);
			this.Controls.Add(this.cmdCancel);
			this.Controls.Add(this.cmdExplore);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.MinimizeBox = false;
			this.Name = "LogsetManagerForm";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Log Manager Configurations";
			this.Click += new System.EventHandler(this.LogsetManagerFormClick);
			this.Load += new System.EventHandler(this.LogsetManagerFormLoad);
			this.groupBox1.ResumeLayout(false);
			this.ResumeLayout(false);
		}
	#endregion
	void CmdOpenClick(object sender, System.EventArgs e)
	{
		OpenSelectedLogset();
	}
	
	void CmdCloseClick(object sender, System.EventArgs e)
	{
		selectedLogset_i = null;
		this.Close();
	}
	
	void CmdNewClick(object sender, System.EventArgs e)
	{
		LogsetWithFile set = new LogsetWithFile();	
		
		if (lvwLogsets.Items.Count > 0  &&  1 == lvwLogsets.SelectedIndices.Count)
		{
			int iIndex = lvwLogsets.SelectedIndices[0];
			LogsetWithFile selected = (LogsetWithFile) logsets_i[iIndex];
			
			if (DialogResult.Yes == MessageBox.Show ("Okey dokey!  Do you want to create a new configuration based on the selected one (" + selected.FileName + ")?", "New configuration", MessageBoxButtons.YesNo))
			{
				set = LogsetLoader.CloneLogsetWithFile(selected);
				set.FileName = null;
			}
		}
		
		EditLogsetForm f = new EditLogsetForm (set);
		f.ShowDialog();
		
		if (f.WasCancelled) return;
		
		logsets_i.Add (set);
//		AddLogsetToList (set);
		
		PopulateLogsetList(logsets_i);
	}
	
	
	
	void CmdEditClick(object sender, System.EventArgs e)
	{
		if (0 == lvwLogsets.Items.Count  ||  0 == lvwLogsets.SelectedIndices.Count) return;
		
		int iIndex = lvwLogsets.SelectedIndices[0];
		LogsetWithFile lwf = (LogsetWithFile) logsets_i[iIndex];
		
		EditLogsetForm f = new EditLogsetForm (lwf);
		f.ShowDialog();
		
		if (f.WasCancelled) return;
		
		PopulateLogsetList(logsets_i);
	}
	
	void CmdDeleteClick(object sender, System.EventArgs e)
	{
		if (0 == lvwLogsets.Items.Count  ||  0 == lvwLogsets.SelectedIndices.Count) return;
		
		int iIndex = lvwLogsets.SelectedIndices[0];
		LogsetWithFile lwf = (LogsetWithFile) logsets_i[iIndex];
		
		if (DialogResult.Yes == MessageBox.Show("Are you sure you want to delete the configuration " + lwf.FileName + "?", this.Text, MessageBoxButtons.YesNo))
		{
			logsets_i.Remove(lwf);
			
			LogsetLoader.DeleteLogset(lwf.FileName);
			PopulateLogsetList(logsets_i);
		}
		
	}
	
	
	
	void LogsetManagerFormLoad(object sender, System.EventArgs e)
	{
		
	}
	
	
	
	
	
	
	private void PopulateLogsetList (IList logsets_p)
	{
		lvwLogsets.Items.Clear();
		
		IEnumerator e = logsets_p.GetEnumerator();
		
		while (e.MoveNext())
		{
			LogsetWithFile l = (LogsetWithFile) e.Current;
			AddLogsetToList (l);
		}	
		
		if (lvwLogsets.Items.Count > 0)
		{
			lvwLogsets.SelectedItems.Clear();
			lvwLogsets.Items[0].Selected = true;				
		}
		
		UpdateButtons();
	}
	
	
	private void AddLogsetToList (LogsetWithFile set_p)
	{	
		String sName = set_p.FileName;
		
		ListViewItem item =  lvwLogsets.Items.Add(sName, 0);		
		
		if (null != set_p.Logset.description)
		{						
			item.SubItems.Add(set_p.Logset.description);
		}		
	}
	
	
	
	private void UpdateButtons()
	{
		bool bLogsetsExist = (lvwLogsets.Items.Count > 0);
		
		
		cmdOpen.Enabled = bLogsetsExist;
		cmdDelete.Enabled = bLogsetsExist;
		cmdEdit.Enabled = bLogsetsExist;
	}
	
	void LogsetManagerFormClick(object sender, System.EventArgs e)
	{
		
	}
	
	void LvwLogsetsDoubleClick(object sender, System.EventArgs e)
	{
		OpenSelectedLogset();
	}

	
	void CmdExploreClick (object sender, System.EventArgs e)
	{
		OpenFileDialog oDialog = new OpenFileDialog();
		
		oDialog.Filter = "Configuration files (*.xml)|*.xml|All files (*.*)|*.*";
		oDialog.FilterIndex = 1;
		oDialog.Multiselect = false;
		oDialog.InitialDirectory = Application.StartupPath;
		
		if (DialogResult.OK == oDialog.ShowDialog())
		{
			string sConfigFileName = oDialog.FileName;

			try
			{
				selectedLogset_i = LogsetLoader.LoadLogsetFromFile (sConfigFileName);						
				this.Close();
			}
			catch (Exception ex)
			{
				MessageBox.Show ("Couldn't open configuration file " + sConfigFileName);
			}			
		}
	}
}
}
